<template>
  <el-container class="frame" style="height: 100%">
    <!-- 侧边栏 -->
    <el-aside :width="isMobile ? '0' : (isCollapse ? '64px' : '240px')" class="my-aside">
      <div class="aside-header">
        <img src="@/assets/images/scau-logo.png" alt="Logo" class="logo-img" />
        <h5 v-if="!isCollapse && !isMobile" class="logo">SCAU - 物业管理系统</h5>
      </div>
      <el-menu
          :collapse="isCollapse"
          class="menu-style"
          default-active="statistics-users"
          @select="pageChange"
      >
        <!-- 数据统计 -->
        <el-sub-menu index="statistics" class="side-menu-item">
          <template #title>
            <span style="color: white">数据统计</span>
          </template>
          <el-menu-item class="side-menu-item" index="statistics-users">用户数据统计</el-menu-item>
          <el-menu-item class="side-menu-item" index="statistics-houses">小区房屋数据统计</el-menu-item>
          <el-menu-item class="side-menu-item" index="statistics-funds">小区资金流数据统计</el-menu-item>
          <el-menu-item class="side-menu-item" index="statistics-complaints">小区投诉与建议数据统计</el-menu-item>
          <el-menu-item class="side-menu-item" index="statistics-parking">小区车位使用数据统计</el-menu-item>
        </el-sub-menu>

        <!-- 管理功能 -->
        <el-sub-menu index="management" class="side-menu-item">
          <template #title>
            <span style="color: white">管理功能</span>
          </template>
          <el-menu-item class="side-menu-item" index="user-management">用户管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="community-management">小区管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="building-management">楼栋管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="house-management">房屋管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="payment-details">缴费详情管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="payment-items">缴费项目管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="complaint-management">投诉与建议管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="notice-management">通知管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="repair-management">报修管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="parking-management">车位管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="parking-use">车位使用管理</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>

    <el-container>
      <!-- 顶部栏 -->
      <el-header class="my-top-nav" :style="{ left: headerLeft }">
        <el-button class="menu-toggle" @click="toggleMenu">
          <el-icon @click="toggleMenu" style="cursor: pointer; margin-right: 16px;">
            <component :is="isCollapse ? MenuUnfold : MenuFold" />
          </el-icon>
        </el-button>
        <div class="user-info">
          <span>欢迎你, {{ userName }}</span>
          <el-button type="info" round @click="handleLogout">退出登录</el-button>
        </div>
      </el-header>

      <!-- 主体内容 -->
      <el-main class="main-content">
        <router-view class="content-area" />
      </el-main>

      <el-footer class="footer">
        <p>&copy;2025 物业管理系统 版权所有</p>
      </el-footer>
    </el-container>
  </el-container>
</template>

<script>
import { ref, computed, onMounted } from 'vue'

export default {
  setup() {
    const isCollapse = ref(false)
    const isMobile = ref(false)

    const checkScreen = () => {
      isMobile.value = window.innerWidth < 768
    }

    const toggleMenu = () => {
      isCollapse.value = !isCollapse.value
    }

    const headerLeft = computed(() => {
      if (isMobile.value) return '0px'
      return isCollapse.value ? '64px' : '240px'
    })

    onMounted(() => {
      checkScreen()
      window.addEventListener('resize', checkScreen)
    })

    return {
      userName: localStorage.getItem('account') || '',
      isCollapse,
      isMobile,
      toggleMenu,
      headerLeft,
      pageChange(url) {
        this.$router.push(url)
      },
      handleLogout() {
        localStorage.clear()
        this.$router.push('/')
      }
    }
  }
}


</script>

<style scoped>
.logo {
  color: white;
  text-align: center;
}

.side-menu-item {
  background-color: #2c3e50;
  color: white; /* 设置字体颜色为白色 */}

/* 白色箭头 */
::v-deep(.el-sub-menu__icon-arrow) {
  color: white !important;
}

::v-deep(.el-sub-menu__title:hover),
::v-deep(.el-menu-item:hover) {
  background-color: black !important;
  color: white !important;
}

.my-aside {
  height: 100vh;
  background-color: #243343;
  overflow: hidden;
  transition: width 0.3s;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}

.aside-header {
  height: 50px;
  display: flex;
  align-items: center;
  padding: 0 10px;
}

.logo-img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: white;
  margin-right: 10px;
}

.my-top-nav {
  position: fixed;
  top: 0;
  right: 0;
  height: 50px;
  padding: 10px 20px;
  background-color: white;
  border-bottom: 1px solid #e5e4e4;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1000;
  transition: left 0.3s ease; /* 添加动画过渡 */
}


.menu-toggle {
  margin-right: 10px;
}

.main-content {
  margin-top: 70px; /* 给 header 腾出空间 */
  margin-left: 240px; /* 留出 aside 空间 */
  transition: margin-left 0.3s;
}

.content-area {
  background-color: #f5f5f5;
  padding: 20px;
  min-height: calc(100vh - 120px);
}

.footer {
  text-align: center;
  padding: 10px;
  margin-left: 240px;
  transition: margin-left 0.3s;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .main-content,
  .footer {
    margin-left: 0 !important;
  }

  .my-aside {
    width: 0 !important;
  }

  .logo {
    display: none;
  }
}

</style>
`
